import {request as Request} from "./Ajax";
import API from "./API";
import {showModal} from "../service/Wx";
import {getStorageSync} from "../utils/Storage";
import CustomError from "../utils/CustomError";
let Questionnaire = {
        /*
    * 获取个人问卷调查列表
    * */
    getPerson: async function () {
        try {
            return await Request({
                url: API.surveyPerson
            })
        } catch (e) {
            throw new CustomError (e, "Questionnaire.getPerson").parse()
        }
    },

    /*
    * 获取个人问卷调查列表
    * */
    getOrg: async function () {
        try {
            return await Request({
                url: API.surveyOrg
            })
        } catch (e) {
            throw new CustomError (e, "Questionnaire.getOrg").parse()
        }
    },

    /*
    * 获取个人问卷调查列表
    * */
    savePerson: async function (data = {}) {
        try {
            return await Request({
                url: API.surveyPersonSave,
                method: "POST",
                data
            })
        } catch (e) {
            throw new CustomError (e, "Questionnaire.savePerson").parse()
        }
    },

    /*
    * 获取个人问卷调查列表
    * */
    saveOrg: async function (data = {}) {
        try {
            return await Request({
                url: API.surveyOrgSave,
                method: "POST",
                data
            })
        } catch (e) {
            throw new CustomError (e, "Questionnaire.saveOrg").parse()
        }
    },

    /*
    * 获取评测积分
    * */
    getResult: async function (isCheckExpired) {
        try {
            let res =  await Request({
                url: API.surveyResult
            }, isCheckExpired);
            return {
                score: res.score,
                level: res.level
            }
        } catch (e) {
            throw new CustomError (e, "Questionnaire.getResult").parse()
        }
    },

    /*
    * 检测是否已测评
    * */
    checkComplete: async function () {
        try {
            let userType = getStorageSync("userType") || getStorageSync("userInfo.type");
            let userInfo = getStorageSync("userInfo");
            if (userType === 3 || userInfo.reg_type !== 1) return true; //产品客户不校验
            let res = await Questionnaire.getResult(true);
            if (!res.score || !res.level) {
                await showModal({
                    title: "提示",
                    content: "请先完成风险调查问卷",
                    showCancel: false,
                    confirmColor: "#b22e2a"
                });
                wx.reLaunch({url: "/packageQuestionnaire/pages/list/list?from=register"})
            }
            return res.score && res.level
        } catch (e) {
            throw new CustomError (e, "Questionnaire.checkComplete").parse()
        }
    }
}
module.exports = Questionnaire